// pages/perCenter/shipAddW/shipAddW.js
var util = require('../../../utils/util.js');
var dateUtil = require('../../../utils/data.js');
var api = require('../../../config/api.js');
var user = require('../../../services/user.js');
var app = getApp();
Page({
  data: {
    name: "",
    telephone: "",
    idCard: "",
    isdefault: false,
    type: 'add',
    id: '',
    index: 0,
    choose:""
  },

  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数

    var isdefault = false;
    if (options.isdefault == "true") {
      isdefault = true
    } else {
      isdefault = false
    }
    if (options.id && options.type == "edit") {
      this.setData({
        id: options.id,
        name: options.name,
        telephone: options.telephone,
        idCard: options.idCard,
        isdefault: isdefault,
        type: 'edit'
      })
    }
    if (options.type == "choose") {
      this.setData({
        id: options.id,
        type: 'edit',
        choose:"choose",
        index: options.index,
        name: options.name,
        telephone: options.telephone,
        idCard: options.idCard,
      })
    }
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },


  setName: function (e) {   //获取input输入的值
    var that = this;
    that.setData({
      name: e.detail.value
    })
  }, setmobile: function (e) {   //获取input输入的值
    var that = this;
    that.setData({
      telephone: e.detail.value
    })
  }, setidCard: function (e) {   //获取input输入的值
    var that = this;
    that.setData({
      idCard: e.detail.value
    })
  }, setDefault: function (e) {   //获取input输入的值
    var that = this;
    if (that.data.isdefault == true) {
      that.setData({
        isdefault: false
      })
    } else {
      that.setData({
        isdefault: true
      })
    }

  },
  save: function () {
    var that = this;
    var obj = new Object();

    obj.id = that.data.id;
    obj.name = that.data.name;
    obj.telephone = that.data.telephone
    obj.idCard = that.data.idCard;
    obj.isDefault = that.data.isdefault;
    let token = wx.getStorageSync('token');

    var mobileRules = /^(13[0-9]|14[5-9]|15[012356789]|166|17[0-8]|18[0-9]|19[8-9])[0-9]{8}$/;
    if (!mobileRules.test(that.data.telephone)) {
      wx.showToast({
        title: '请输入正确的手机号码',
        icon: 'none'
      })
      return
    }
    if (that.data.name == "") {
      wx.showToast({
        title: '名称不能为空',
        icon: 'none'
      })
      return
    } if (that.data.idCard == "") {
      wx.showToast({
        title: '名称不能为空',
        icon: 'none'
      })
      return
    }


    wx.showLoading({
      title: '保存中',
    })
    util.request(api.ContactsOper, { token: token, contacts: obj, type: that.data.type }).then(function (res) {
      wx.hideLoading();
      if (res.code === '200') {
        if (that.data.choose == "choose") {
          var pages = getCurrentPages();
          var prevPage = pages[pages.length - 2]; //上一个页面
          var predata = prevPage.data.userList;
          predata[that.data.index].name = that.data.name
          predata[that.data.index].telephone = that.data.telephone
          predata[that.data.index].idCard = that.data.idCard
          prevPage.setData({
            userList: predata
          });
          wx.navigateBack({
            delta: 1 // 回退前 delta(默认为1) 页面
          })
        } else {

          wx.navigateBack({

          })
        }
      }
    });
  }
})



